import localStorage from '@/common/localstorage/localstorage.js'
let cart_name = "cart_shopping_key"

let setCartNumfn = function(count) {
	if (count > 0) {
		uni.setTabBarBadge({
			index: 2,
			text: count.toString()
		})
	}else{
		uni.removeTabBarBadge({
			index: 2
		})
	}
}
export default ({
	cartNum: 0,
	getCartVal() {
		let cartVal = localStorage.getStorageSync({
			key: cart_name
		})
		setCartNumfn(cartVal)
		return cartVal
	},
	setCartVal(val) {
		let cartVal = localStorage.getStorageSync({
			key: cart_name
		})
		let count = parseInt(cartVal || 0);
		count += val;
		
		if(count<=0) count=0;
		
		localStorage.setStorage({
			key: cart_name,
			jsonstr: count
		})
		setCartNumfn(count)
	},
	delCartVal() {
		let cartVal = localStorage.getStorageSync({
			key: cart_name
		})
		let count = parseInt(cartVal || 0);
		count--;
		localStorage.setStorage({
			key: cart_name,
			jsonstr: count
		})
		setCartNumfn(count)
	}
})
